package com.example.asapp.rect;


import com.example.asapp.rect.vos.Rect;
import com.example.asapp.rect.vos.RectRq1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.UUID;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@RestController
@Slf4j
@RequestMapping("api/rect")
public class RectApi {

    @Autowired
    RectDao rectDao;

    @PostMapping("insertRects")
    public Integer insertKeys(@RequestBody RectRq1 rectRq1){
        log.info("插入信息是:{}",rectRq1);
        AtomicReference<Integer> i= new AtomicReference<>(0);
        String rectId=UUID.randomUUID().toString();
        String name=rectRq1.getName();
        List<Rect> rectList=rectRq1.getRectList();
        rectList.parallelStream().forEach(e->{
            e.setId(UUID.randomUUID().toString());
            e.setRectId(rectId);
            e.setName(name);
            rectDao.save(e);
            i.updateAndGet(v -> v + 1);
        });
        return i.get();
    }


    @Autowired
    RectMapper rectMapper;

    @GetMapping("getAll")
    public List<Rect> getRects(){
        List<Rect> rects=rectMapper.findDistinctName();
        log.info("results:{}",rects);
        return rects;
    }

    @GetMapping("getById/{id}")
    public List<HashMap<String,Object>> getById(@PathVariable String id){
        return rectMapper.findAllByRectId(id);
    }
}
